package com.wgke.bluetooth.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wgke.bluetooth.db.CmdEntity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class CommonUtil {

    private static Context context;

    public static Context getContext() {
        return context;
    }

    public static void setContext(Context context) {
        CommonUtil.context = context;
    }


    public static boolean isActivityTop(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager == null) return false;

        List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
        if (tasks == null || tasks.isEmpty()) return false;

        ActivityManager.RecentTaskInfo taskInfo = tasks.get(0).getTaskInfo();
        if (taskInfo == null) return false;

        return taskInfo.topActivity != null
                && taskInfo.topActivity.equals(((Activity)context).getComponentName());
    }

    public static <T> List<T> parseJsonArray(String json, Class<T> clazz) {
        try {
            Gson gson = new Gson();
            // 使用TypeToken处理泛型类型
            Type type = TypeToken.getParameterized(List.class, clazz).getType();
            List<T> result = gson.fromJson(json, type);
            // 防止返回null
            return result != null ? result : new ArrayList<>();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    public static long getVersionCode(Context context){
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            long versionCode = pInfo.getLongVersionCode();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static int[] rangeStringToIntArray(String rangeStr) {
        try {
            // 分割字符串获取最小值和最大值
            String[] parts = rangeStr.split("-");
            if (parts.length != 2) {
                throw new IllegalArgumentException("Invalid range format " +rangeStr);
            }
            int min = Integer.parseInt(parts[0].trim());
            int max = Integer.parseInt(parts[1].trim());
            // 确保范围有效
            if (min > max) {
                throw new IllegalArgumentException("Min cannot be greater than max");
            }
            // 创建并填充数组
            int[] result = new int[max - min + 1];
            for (int i = 0; i < result.length; i++) {
                result[i] = min + i;
            }
            return result;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid number in range", e);
        }
    }
 }
